package com.daisyday.demo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public final static SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 获取上周五时间
     */
    public Date lastFirday() {

        //作用防止周日得到本周日期
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 7 - dayOfWeek;
        calendar.add(Calendar.DATE, offset - 9);

        return null;

    }

    public static void main(String[] args) {
        System.out.println("6230582000070264000".length());
        System.out.println("6232610110000483455".length());
        //作用防止周日得到本周日期
        Calendar calendar = Calendar.getInstance();
        Date time = calendar.getTime();
        System.out.println(dft.format(time));
        //获取周一
        // 作用防止周日得到本周日期
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
            System.out.println(dft.format(calendar.getTime()));
        }
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        System.out.println(dayOfWeek);
        /**
         *  1,2,3,4,5,6,7
         *
         */
        int offset = 7 - dayOfWeek;
        calendar.add(Calendar.DATE, offset - 9);
        //这是从上周日开始数的到本周五为6
        Date firstDayOfWeek = getFirstDayOfWeek(calendar.getTime(), 6);
        System.out.println(dft.format(firstDayOfWeek));
    }


    /**
     * 得到某一天的该星期的第一日 00:00:00
     *
     * @param date
     * @param firstDayOfWeek 一个星期的第一天为星期几
     * @return
     */
    public static Date getFirstDayOfWeek(Date date, int firstDayOfWeek) {
        Calendar cal = Calendar.getInstance();
        if (date != null) cal.setTime(date);
        cal.setFirstDayOfWeek(firstDayOfWeek);//设置一星期的第一天是哪一天
        cal.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);//指示一个星期中的某天
        cal.set(Calendar.HOUR_OF_DAY, 0);//指示一天中的小时。HOUR_OF_DAY 用于 24 小时制时钟。例如，在 10:04:15.250 PM 这一时刻，HOUR_OF_DAY 为 22。
        cal.set(Calendar.MINUTE, 0);//指示一小时中的分钟。例如，在 10:04:15.250 PM 这一时刻，MINUTE 为 4。
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}
